<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: test_hashtable.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>test_hashtable.c</h1><a href="test__hashtable_8c.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00005"></a>00005 
<a name="l00006"></a>00006 <span class="preprocessor">#include "<a class="code" href="hash__table_8h.html">hash_table.h</a>"</span>
<a name="l00007"></a>00007 
<a name="l00008"></a>00008 <span class="comment">/* suppose a user wants to insert stuff</span>
<a name="l00009"></a>00009 <span class="comment"> * into the hash table all she needs to do</span>
<a name="l00010"></a>00010 <span class="comment"> * is make sure hash_entry is part of the item</span>
<a name="l00011"></a>00011 <span class="comment"> *</span>
<a name="l00012"></a>00012 <span class="comment"> * Note: a single hash_table can contain multiple</span>
<a name="l00013"></a>00013 <span class="comment"> * types as the hash_table is oblivious to user defined</span>
<a name="l00014"></a>00014 <span class="comment"> * types. However, at run time you need to know/guess the</span>
<a name="l00015"></a>00015 <span class="comment"> * type of data each hash_entry is holding. You can, for</span>
<a name="l00016"></a>00016 <span class="comment"> * example, reserve the first byte of your data for holding</span>
<a name="l00017"></a>00017 <span class="comment"> * this type info.</span>
<a name="l00018"></a>00018 <span class="comment"> */</span>
<a name="l00019"></a><a class="code" href="structmyitem.html">00019</a> <span class="keyword">struct </span><a class="code" href="structmyitem.html">myitem</a> {
<a name="l00020"></a><a class="code" href="structmyitem.html#8bed4425521a7051df60d34b48feeb0f">00020</a>         <span class="keyword">struct </span><a class="code" href="structhash__entry.html">hash_entry</a> <a class="code" href="structmyitem.html#8bed4425521a7051df60d34b48feeb0f">entry</a>;
<a name="l00021"></a><a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">00021</a>         <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">foo</a>[32];
<a name="l00022"></a><a class="code" href="structmyitem.html#d9b70ec90d7aceb2f8d47484a99e414c">00022</a>         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="structmyitem.html#d9b70ec90d7aceb2f8d47484a99e414c">bar</a>;
<a name="l00023"></a>00023 };
<a name="l00024"></a>00024 
<a name="l00025"></a><a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">00025</a> <span class="keywordtype">int</span> <a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
<a name="l00026"></a>00026 {
<a name="l00027"></a>00027         <span class="keyword">struct </span><a class="code" href="structmyitem.html">myitem</a> *tmp;
<a name="l00028"></a>00028         <span class="keyword">struct </span><a class="code" href="structhash__table.html">hash_table</a> mytable;
<a name="l00029"></a>00029         <span class="keyword">struct </span><a class="code" href="structhash__entry.html">hash_entry</a> *hentry;
<a name="l00030"></a>00030         <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> str[256];
<a name="l00031"></a>00031         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, i, flowid;
<a name="l00032"></a>00032 
<a name="l00033"></a>00033         <span class="comment">/* initialize the hash table with 10 buckets */</span>
<a name="l00034"></a>00034         <a class="code" href="hash__table_8h.html#7e37848bc7e0e4fe223f633949331aa4">hash_table_init</a>(&amp;mytable, 10, NULL);
<a name="l00035"></a>00035 
<a name="l00036"></a>00036         fprintf(stdout, <span class="stringliteral">"input\n"</span>);
<a name="l00037"></a>00037         <span class="keywordflow">for</span> (i = 0; i &lt; 5; ++i) {
<a name="l00038"></a>00038                 fprintf(stdout, <span class="stringliteral">"key: "</span>);
<a name="l00039"></a>00039                 fscanf(stdin, <span class="stringliteral">"%s"</span>, str);
<a name="l00040"></a>00040                 len = strlen(str);
<a name="l00041"></a>00041                 fprintf(stdout, <span class="stringliteral">"flowid: "</span>);
<a name="l00042"></a>00042                 fscanf(stdin, <span class="stringliteral">"%d"</span>, &amp;flowid);
<a name="l00043"></a>00043 
<a name="l00044"></a>00044                 tmp = (<span class="keyword">struct </span><a class="code" href="structmyitem.html">myitem</a> *)malloc(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structmyitem.html">myitem</a>));
<a name="l00045"></a>00045                 memset(tmp, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structmyitem.html">myitem</a>));
<a name="l00046"></a>00046                 memcpy(tmp-&gt;<a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">foo</a>, str, len);
<a name="l00047"></a>00047                 tmp-&gt;<a class="code" href="structmyitem.html#d9b70ec90d7aceb2f8d47484a99e414c">bar</a> = flowid;
<a name="l00048"></a>00048 
<a name="l00049"></a>00049                 <span class="comment">/* insert this entry into hash table mytable with</span>
<a name="l00050"></a>00050 <span class="comment">                 * str (of length len) as the key</span>
<a name="l00051"></a>00051 <span class="comment">                 */</span>
<a name="l00052"></a>00052                 <a class="code" href="hash__table_8h.html#b169c085175842723f0b8ef86d138f0c">hash_table_insert</a>(&amp;mytable, &amp;tmp-&gt;<a class="code" href="structmyitem.html#8bed4425521a7051df60d34b48feeb0f">entry</a>, str, len);
<a name="l00053"></a>00053         }
<a name="l00054"></a>00054 
<a name="l00055"></a>00055         <span class="keywordflow">for</span> (i = 0; i &lt; 3; ++i) {
<a name="l00056"></a>00056                 fprintf(stdout, <span class="stringliteral">"enter key to search:"</span>);
<a name="l00057"></a>00057                 fscanf(stdin, <span class="stringliteral">"%s"</span>, str);
<a name="l00058"></a>00058 
<a name="l00059"></a>00059                 <span class="keywordflow">if</span> ((hentry =
<a name="l00060"></a>00060                      <a class="code" href="hash__table_8h.html#26234bcfb00e679df2eb4b8e1476a9d5">hash_table_lookup_key</a>(&amp;mytable, str,
<a name="l00061"></a>00061                                            strlen(str))) == NULL) {
<a name="l00062"></a>00062                         fprintf(stdout, <span class="stringliteral">"could not find entry for %s\n"</span>, str);
<a name="l00063"></a>00063                 } <span class="keywordflow">else</span> {
<a name="l00064"></a>00064                         <span class="comment">/* just like the list_item() */</span>
<a name="l00065"></a>00065                         tmp = <a class="code" href="hash__table_8h.html#6d96b1b753cb4969aaa7f782021bcf5b" title="hash_entry - get the user data for this entry : the &amp;amp;struct hash_entry pointer...">hash_entry</a>(hentry, <span class="keyword">struct</span> <a class="code" href="structmyitem.html">myitem</a>, <a class="code" href="structmyitem.html#8bed4425521a7051df60d34b48feeb0f">entry</a>);
<a name="l00066"></a>00066                         fprintf(stdout, <span class="stringliteral">"key=%s\tfoo=%s\tbar=%d\n"</span>,
<a name="l00067"></a>00067                                 str, tmp-&gt;<a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">foo</a>, tmp-&gt;<a class="code" href="structmyitem.html#d9b70ec90d7aceb2f8d47484a99e414c">bar</a>);
<a name="l00068"></a>00068                 }
<a name="l00069"></a>00069         }
<a name="l00070"></a>00070 
<a name="l00071"></a>00071         <span class="comment">/* loop thru all the entries in the hash-table */</span>
<a name="l00072"></a>00072         fprintf(stdout, <span class="stringliteral">"output\n"</span>);
<a name="l00073"></a>00073         <a class="code" href="hash__table_8h.html#ca4be66f0e2f9faa7c47324d54a7c172">hash_table_for_each</a>(hentry, &amp;mytable) {
<a name="l00074"></a>00074                 tmp = <a class="code" href="hash__table_8h.html#6d96b1b753cb4969aaa7f782021bcf5b" title="hash_entry - get the user data for this entry : the &amp;amp;struct hash_entry pointer...">hash_entry</a>(hentry, <span class="keyword">struct</span> <a class="code" href="structmyitem.html">myitem</a>, <a class="code" href="structmyitem.html#8bed4425521a7051df60d34b48feeb0f">entry</a>);
<a name="l00075"></a>00075                 fprintf(stdout, <span class="stringliteral">"key: %s[%d] (%d)\n"</span>, tmp-&gt;<a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">foo</a>, tmp-&gt;<a class="code" href="structmyitem.html#d9b70ec90d7aceb2f8d47484a99e414c">bar</a>,
<a name="l00076"></a>00076                         <a class="code" href="hash__table_8h.html#b80477ad6e8ad63b4ea83252c67392f1">hash_table_hash_code</a>(&amp;mytable, tmp-&gt;<a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">foo</a>,
<a name="l00077"></a>00077                                              strlen(tmp-&gt;<a class="code" href="structmyitem.html#36fe509542e0a1e9cecbb7a53640f46e">foo</a>)));
<a name="l00078"></a>00078         }
<a name="l00079"></a>00079 
<a name="l00080"></a>00080         <span class="comment">/* finitialze the hash table */</span>
<a name="l00081"></a>00081         <a class="code" href="hash__table_8h.html#506d39e2faf00001f5354fd67ffaf590">hash_table_finit</a>(&amp;mytable);
<a name="l00082"></a>00082         <span class="keywordflow">return</span> 0;
<a name="l00083"></a>00083 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:25 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
